package com.blensmile.activity;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blensmile.BaseActivity;
import com.blensmile.R;

import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CheckFormActivity extends BaseActivity implements View.OnClickListener {

    private static final int TEXT_COLOR = 0xffbbbbbb;
    private static final int BACK_BG = 0xff54a2fc;
    private static final String TV_C1 = "tv_c1";
    private static final String TV_C2 = "tv_c2";
    private static final String TV_V1 = "tv_v1";
    private static final String TV_V2 = "tv_v2";
    private static final String TV_P1 = "tv_p1";
    private static final String TV_P2 = "tv_p2";
    @BindView(R.id.et_edit1)
    EditText etEdit1;


    private TextView tvC1;
    private TextView tvC2;
    private TextView tvP1;
    private TextView tvP2;
    private TextView tvV1;
    private TextView tvV2;
    private EditText etEdit;

    private Set<String> reasonSet = new HashSet<>();
    private LinearLayout llWraper;
    private LinearLayout ll_edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_form);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        tvC1 = (TextView) findViewById(R.id.tv_c1);
        tvC1.setOnClickListener(this);
        tvC2 = (TextView) findViewById(R.id.tv_c2);
        tvC2.setOnClickListener(this);
        tvP1 = (TextView) findViewById(R.id.tv_p1);
        tvP1.setOnClickListener(this);
        tvP2 = (TextView) findViewById(R.id.tv_p2);
        tvP2.setOnClickListener(this);
        tvV1 = (TextView) findViewById(R.id.tv_v1);
        tvV1.setOnClickListener(this);
        tvV2 = (TextView) findViewById(R.id.tv_v2);
        tvV2.setOnClickListener(this);
        tvC1.getBackground().setColorFilter(TEXT_COLOR, PorterDuff.Mode.SRC_IN);
        tvC2.getBackground().setColorFilter(TEXT_COLOR, PorterDuff.Mode.SRC_IN);
        tvP1.getBackground().setColorFilter(TEXT_COLOR, PorterDuff.Mode.SRC_IN);
        tvP2.getBackground().setColorFilter(TEXT_COLOR, PorterDuff.Mode.SRC_IN);
        tvV1.getBackground().setColorFilter(TEXT_COLOR, PorterDuff.Mode.SRC_IN);
        tvV2.getBackground().setColorFilter(TEXT_COLOR, PorterDuff.Mode.SRC_IN);
        etEdit = (EditText) findViewById(R.id.et_edit);
        etEdit.setOnClickListener(this);

        etEdit1.setOnClickListener(this);
        llWraper = (LinearLayout) findViewById(R.id.ll_wraper);
        llWraper.setOnClickListener(this);
        ll_edit = (LinearLayout) findViewById(R.id.ll_edit);
        ll_edit.setOnClickListener(this);
    }

//    private SpannableString getImageSpan(String text){
//        if(text == null){
//            text = "";
//        }
//        Drawable drawable = getResources().getDrawable(R.drawable.homework);
//        int height = DensityUtils.dip2px(this,etEdit1.getTextSize() - 4);
//        drawable.setBounds(0,0,height,height);
//        SpannableString spannableString = new SpannableString(text);
//        spannableString.setSpan(new ImageSpan(drawable),text.length()-1,text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        return spannableString;
//    }

    private void hideKeyboard() {
        if (getCurrentFocus() != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() != R.id.et_edit1 && v.getId() != R.id.ll_edit) {
            hideKeyboard();
            etEdit1.setCursorVisible(false);
        }
        switch (v.getId()) {
            case R.id.tv_c1:
                setTextView(tvC1, TV_C1);
                break;
            case R.id.tv_c2:
                setTextView(tvC2, TV_C2);
                break;
            case R.id.tv_v1:
                setTextView(tvV1, TV_V1);
                break;
            case R.id.tv_v2:
                setTextView(tvV2, TV_V2);
                break;
            case R.id.tv_p1:
                setTextView(tvP1, TV_P1);
                break;
            case R.id.tv_p2:
                setTextView(tvP2, TV_P2);
                break;
            case R.id.ll_edit:
                etEdit1.requestFocus();
                etEdit1.setCursorVisible(true);
                etEdit1.setSelection(etEdit1.getText().toString().length());
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.showSoftInput(etEdit1,0);
                break;
            case R.id.et_edit1:
                etEdit1.setSelection(etEdit1.getText().toString().length());
                etEdit1.requestFocus();
                etEdit1.setCursorVisible(true);
                break;
        }
    }

    private void setTextView(TextView textView, String str) {
        if (reasonSet.contains(str)) {
            reasonSet.remove(str);
            textView.setTextColor(TEXT_COLOR);
            textView.setBackgroundResource(R.drawable.oblong_edge);
            textView.getBackground().setColorFilter(TEXT_COLOR, PorterDuff.Mode.SRC_IN);
        } else {
            reasonSet.add(str);
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundResource(R.drawable.oblong_fill);
            textView.getBackground().setColorFilter(BACK_BG, PorterDuff.Mode.SRC_IN);
        }
    }

    private void submit() {
        // validate
//        String edit = et_edit.getText().toString().trim();
//        if (TextUtils.isEmpty(edit)) {
//            Toast.makeText(this, "helelo", Toast.LENGTH_SHORT).show();
//            return;
//        }

        // TODO validate success, do something


    }

}
